package com.bringway.p2p.controller;

import java.io.OutputStream;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.bringway.framework.controller.JsonController;
import com.bringway.framework.excel.ExcelOperUtil;
import com.bringway.framework.excel.ExcelSheet;
import com.bringway.framework.util.DateUtils;
import com.bringway.framework.util.RequestUtils;
import com.bringway.global.domain.SysUserDetails;
import com.bringway.p2p.service.VipService;
import net.sf.json.JSONObject;

/*
 * @author fan
 * @className VipController
 * @description vip操作控制器
 * @date 2015年12月9日上午10:10:37
 */
@Controller
@RequestMapping("/vip")
public class VipController extends JsonController {

	@Autowired
	private VipService vipService;

	
	/**
	 * 
	 * @param request vip查询
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/vipcz/queryVipCzList.do")
	public ModelAndView vipczQuery(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String startDate = RequestUtils.getStringParameter(request, "startDate");
		String endDate = RequestUtils.getStringParameter(request, "endDate");
		String tjr = RequestUtils.getStringParameter(request, "TJR");
		String username = RequestUtils.getStringParameter(request, "USERNAME");
		String sjhm = RequestUtils.getStringParameter(request, "SJHM");
		Integer start = RequestUtils.getIntParameter(request, "start");
		Integer limit = RequestUtils.getIntParameter(request, "limit");
		Integer page = RequestUtils.getIntParameter(request, "page");//新版本,传参
		if (null!=page) {//新版本,传参
			start=(page-1)*limit;
		}
		JSONObject jo = new JSONObject();
		JSONObject result = new JSONObject();
		jo.put("startDate", startDate);
		jo.put("endDate", endDate);
		jo.put("tjr", tjr);
		jo.put("start", start);
		jo.put("limit", limit);
		jo.put("username", username);
		jo.put("sjhm", sjhm);
		List list = vipService.vipczQuery(jo);
		int total = vipService.getPageCount(jo);
		result.put("data", list);
		result.put("total", total);
		if (null!=page) {//新版本,传参
			int totalPage=total%limit==0?total/limit:total/limit+1;
			result.put("totalPage", totalPage);
			
		}
		serialize(request, response, result);
		return null;
	}
	@RequestMapping("/vipcz/exportVipCzExcelMethod")
	public ModelAndView exportVipCzExcelMethod(HttpServletRequest request, HttpServletResponse response) throws Exception{
		String startDate = RequestUtils.getStringParameter(request, "startDate");
		String endDate = RequestUtils.getStringParameter(request, "endDate");
		String tjr = RequestUtils.getStringParameter(request, "TJR");
		String username = RequestUtils.getStringParameter(request, "USERNAME");
		String sjhm = RequestUtils.getStringParameter(request, "SJHM");
		JSONObject params = new JSONObject();
		params.put("startDate", startDate);
		params.put("endDate", endDate);
		params.put("tjr", tjr);
		params.put("username", username);
		params.put("sjhm", sjhm);
		// 查询数据
		List list = vipService.vipczQuery(params);
		List<String[]> dataList = com.bringway.p2p.constant.ExcelHeadConstant.getVipCzExportSearchResultData(list);
		
		// 导出数据到Excel文件
		List<ExcelSheet> sheetList = new ArrayList<ExcelSheet>();
		ExcelSheet sheet = new ExcelSheet();
		sheet.setBodyList(dataList);
		sheet.setSheetName(DateUtils.getCurrentDataStr()+"VIP充值查询");
		sheetList.add(sheet);
		OutputStream os = response.getOutputStream();
		String fileName = DateUtils.getCurrentDataStr() + "VIP充值查询.xls";
		super.setDownLoadResponseHeader(request, response, fileName);
		response.setContentType("application/vnd.ms-excel");
		
		// 导出EXCEL
		ExcelOperUtil.writeExcel(sheetList, os);
		return null;
	}
}
